package citic.cph.server.test.service.impl;

import citic.cph.server.test.dao.entity.GenTableColumn;
import citic.cph.server.test.dao.entity.TableDefine;
import citic.cph.server.test.dao.mapper.GenTableColumnMapper;
import citic.cph.server.test.dao.mapper.TableDefineMapper;
import citic.cph.server.test.service.ITableDefineService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author SPL
 * @since 2021-10-27
 */
@Service
@Slf4j
public class TableDefineServiceImpl extends ServiceImpl<TableDefineMapper, TableDefine> implements ITableDefineService {

    @Resource
    private TableDefineMapper tableDefineMapper;
    @Resource
    private GenTableColumnMapper genTableColumnMapper;

    /**
     * 分页查询列表
     *
     * @param tableDefine
     * @return 列表
     */
    @Override
    public IPage<TableDefine> selectPageList(Page<TableDefine> page, TableDefine tableDefine) {
        return tableDefineMapper.selectPageList(page, tableDefine);
    }

    @Override
    public void init(String tableName) {
        List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
        log.info("{}", genTableColumns);
        genTableColumns.forEach(i->{
            TableDefine build = TableDefine.builder()
                    .tName(tableName)
                    .field(i.getColumnName())
                    .comment(i.getColumnComment())
                    .dataType(i.getColumnType())
                    .build();
            tableDefineMapper.insert(build);
        });
    }

}
